//
//  YBApiManager+Global.m
//  ChangChat
//
//  Created by Cocoa on 2019/9/3.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "YBApiManager+Global.h"
#import "UIImage+Utils.h"
#import <AFURLRequestSerialization.h>
#import <SDImageCache.h>
#import "amrFileCodec.h"
#import "CacheHelper.h"

typedef void(^RequestUploadMultiMediaBlock)(id <AFMultipartFormData> formData);
@implementation YBApiManager (Global)


- (RACSignal *)bindUserIdWithCliendId:(NSString *)cliendId ds:(NSString *)ds get:(BOOL)get {
    NSDictionary *param = @{@"client_id": cliendId,
//                            @"get": @(get)
                            };
//    if (get == YES) {
//        param = @{@"client_id": cliendId,
//                  @"ds": ds?:@"[]",
//                  @"get": @(get)
//                  };
//    }
    return [self sendGetRequest:@"index/index" param:param];
}

- (RACSignal *)uploadImgWithImage:(UIImage *)img {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:[self constructingBodyBlock:img] forKey:Rest_MultiMediaBlock];
    
    return [self sendPostRequest:@"common/upload" param:param];
}
- (RequestUploadMultiMediaBlock)constructingBodyBlock:(UIImage *)image {
    return ^(id<AFMultipartFormData> formData){
        NSData *data = [image zipToMaxSize:1000];//不能超过1M
        NSString *name = @"file";//指定
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]];
        NSString *type = @"image/jpg";
        [formData appendPartWithFileData:data name:name fileName:fileName mimeType:type];
    };
}

- (RACSignal *)uploadFileWithFilePath:(NSString *)filePath type:(MsgType)type {
    
    
    
    NSString *mimetype = nil;
    NSData *fileData = nil;
    NSError *error = nil;
    if (type == MsgType_Photo) {
        mimetype = @"image/jpg";
        fileData = [[SDImageCache sharedImageCache] diskImageDataForKey:filePath];
        
    }
    else if (type == MsgType_Voice) {
        mimetype = @"amr";
        fileData = [NSData dataWithContentsOfURL:[Cache_Help allFilePathWithMsgType:type fileName:filePath].fileToURL options:NSDataReadingUncached error:&error];
        fileData = EncodeWAVEToAMR(fileData, 1, 16);
        
    } else if (type == MsgType_Video) {
        mimetype = @"mp4";
        fileData = [NSData dataWithContentsOfURL:[Cache_Help allFilePathWithMsgType:type fileName:filePath].fileToURL options:NSDataReadingUncached error:&error];
    } else if (type == MsgType_File) {
        mimetype = [filePath componentsSeparatedByString:@"."].lastObject;
        fileData = [NSData dataWithContentsOfURL:[Cache_Help allFilePathWithMsgType:type fileName:filePath].fileToURL options:NSDataReadingUncached error:&error];
    }
    if (error) {
        NSLog(@"%@",error);
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    id block = [self constructingBody_fileBlock:fileData type:mimetype];
    [param setObject:block forKey:Rest_MultiMediaBlock];
    return [self sendPostRequest:@"common/upload" param:param];
}

- (RequestUploadMultiMediaBlock)constructingBody_fileBlock:(NSData *)fileData type:(NSString *)type {
    return ^(id<AFMultipartFormData> formData){
        NSData *data = fileData;
        NSString *name = @"file";//指定
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        
        
        
        NSString *fileName = [NSString stringWithFormat:@"%@.%@",[formatter stringFromDate:[NSDate date]], [type componentsSeparatedByString:@"/"].lastObject];
        NSString *mimetype = type;
        [formData appendPartWithFileData:data name:name fileName:fileName mimeType:mimetype];
    };
}
@end
